package com.example.recruitment_website.controller;

import com.example.recruitment_website.domain.User;
import com.example.recruitment_website.repository.UserRepository;
import com.example.recruitment_website.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;


@RestController
@RequestMapping("User")
public class UserController {
    @Autowired
    UserRepository userRepository;
    @Autowired
    UserService userService;

    @GetMapping("/findById")
    public HashMap findRecordById(String id){

        User user = userRepository.findByOpen_id(id);
        HashMap<String,Object> map = new HashMap<>();
        map.put("msg","添加成功");
        map.put("data",user);
        map.put("code",1);
        return map;
    }

    @GetMapping("/login")
    public HashMap login(String openId,String name,String imgUrl){
        HashMap<String,Object> map = new HashMap<>();
        if (openId==null||name==null||imgUrl==null){
            map.put("msg","登录失败，请确保参数完整");
            map.put("code",0);
            return map;
        }

        int userId = userService.login(name,imgUrl,openId);
        if(userId == 0){
            map.put("msg","登录失败");
            map.put("code",0);
        }else{
            map.put("msg","登录成功");
            map.put("data",userId);
            map.put("code",1);
        }
        return map;
    }
}
